using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.Skill
{
    /// <summary>
    /// 根据普攻射击动作+IK+自定义旋转角度和速度的扫射
    /// </summary>
    public class WaveShoot:SkillCasterBase
    {
        public override void OnReset()
        {
            base.OnReset();
            if (aimTransform != null)
            {
                GameObject.Destroy(aimTransform.gameObject);
            }
        }

        public override void Init(SkillBase skill, PveSkillConfig config, int childSkillIndex)
        {
            base.Init(skill, config, childSkillIndex);
            if (aimTransform == null)
            {
                var obj = new GameObject("WaveShootToken");
                aimTransform = obj.transform;
                aimTransform.SetParent(skill.unit.gameObject.transform);
            }
        }

        public override void Start(float stateStartTime)
        {
            base.Start(stateStartTime);
            nextShootTime = 0;
            shootIntetval = config.GetCastParam(0);
            shootAngle = config.GetCastParam(1);
            shootAngleSpeed = config.GetCastParam(2);
            var offset = skill.GetBulletOffset(config.bullets[0].z);
            var direction = new Vector3(offset.x, 0, offset.z);
            startForward = skill.unit.Data.forward;
            height = offset.y;
            aimTransform.transform.position = skill.unit.Data.position + new Vector3(0,height,0) + GetForwardByTime(0) * 3;
            currentAngle = 0;
            skill.unit.Data.aimTransform = aimTransform;
            skill.unit.Transmit(ETransmitType.AimTransform);
        }

        public override void End()
        {
            skill.unit.Data.aimTransform = null;
            skill.unit.Transmit(ETransmitType.CancelAim);
            base.End();
        }

        private Vector3 GetForwardByTime(float deltaTime)
        {
            var angleMax = shootAngle / 2;
            var angleMin = -shootAngle / 2;
            var angle = deltaTime * shootAngleSpeed;
            currentAngle += angle;
            if (currentAngle > angleMax)
            {
                currentAngle = angleMax;
                shootAngleSpeed = -shootAngleSpeed;
            }
            else if (currentAngle < angleMin)
            {
                currentAngle = angleMin;
                shootAngleSpeed = -shootAngleSpeed;
            }

            return Quaternion.AngleAxis(currentAngle, Vector3.up) * startForward;
        }

        private Transform aimTransform;

        private float height;
        private Vector3 startForward;
        private float lastTime;
        private float currentAngle;
        private float shootAngleSpeed;
        private float shootAngle;
        private float shootIntetval;
        private float nextShootTime;
        
        public override void Update(float currentTime)
        {
            aimTransform.transform.position = skill.unit.Data.position + new Vector3(0,height,0) + GetForwardByTime(currentTime - lastTime) * 3;
            if (nextShootTime == 0 || TimeManager.logicTime >= nextShootTime)
            {
                var offset = skill.GetBulletOffset((int)config.bullets[0].z);
                var forward = new Vector3(offset.x, 0, offset.z).normalized;
                forward.y = Random.Range(-0.1f, 0.1f);
                
                skill.CreateBulletByRotation(offset,forward,(int)config.bullets[0].y,0,config.bullets[0].z);
                nextShootTime = TimeManager.logicTime + shootIntetval;
            }
            lastTime = currentTime;
        }
    }
}